<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta
		  name="viewport"
		  content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover"
		/>
		<!-- <meta content="maximum-scale=1.2,user-scalable=yes" name="viewport"> -->
		<link rel="stylesheet" href="css/jsoneditor.min.css" type="text/css">
		<script src="js/jsoneditor.min.js"></script>
		<script type="text/javascript" src="js/uni-webview-js@1.5.4.js"></script>
		<style>
			html, body {
				margin: 0;
				padding: 0;
			}
			.jsoneditor {
				width: 100vw;
				height: 100vh;
			}
			.jsoneditor-menu {
			  display: none !important;
			}
		</style>
	</head>
	<body>
		<div id="jsoneditor" class="jsoneditor"></div>
	</body>
	<script>
		var container = document.getElementById('jsoneditor');
		var editor = new JSONEditor(container, {
		  mode: 'code', // 可选 'code'、'form'、'text'、'tree'、'view'
		  theme: 'light',
		  mainMenuBar: false,
		  onChange: function (e) {
			uni.postMessage({ data: { change: editor.getText() } });
		  },
		  onError: function (err) {
			uni.postMessage({ data: { error: err.toString() } });
		  }
		});
		function setValue (value) {
			var text = decodeURIComponent(decodeURIComponent(value.replace(/&aposync;/g, "'")))
			editor && editor.set(JSON.parse(text))
		}
		function undo () {
			if ( editor ) {
				const aceEditor = editor.aceEditor;
				aceEditor.undo()
			}
		}
		function redo() {
			if ( editor ) {
				const aceEditor = editor.aceEditor;
				aceEditor.redo()
			}
		}
		function insert (value) {
			if ( editor ) {
				const aceEditor = editor.aceEditor;
				aceEditor.insert(decodeURIComponent(decodeURIComponent(value.replace(/&aposync;/g, "'"))));
			}
		}
		function format () {
			if ( editor ) {
				try{
					const aceEditor = editor.aceEditor;
					const json = editor.get();
					editor.set(json);
				}catch(e){
					alert('格式化失败请检查JSON格式是否正确，错误原因：' + e.toString())
				}
			}
		}
		function setMode (mode) {
			if ( editor ) editor.setMode(mode)
		}
		function setTheme (theme) {
			document.body.style.filter = theme == 'dark' ? 'invert(1)' : ''
		}
		window.onload = function () {
			uni.postMessage({ data: { ready: 1 } });
		}
	</script>
</html>